<?php

#--------------------------------------------------
# ALL INCLUDES + STANDARD-VALUES
#--------------------------------------------------
include_once ("include/config.inc");
include_once ("include/template.inc");
include_once ("include/class.db.php5");
include_once ("include/class.tools.php5");
$svo = new db("svoptik");
$tools = new tool();

global $team_id, $complete;
include_once ("stats_inc.php5");

include_once ("include/global_fix_extern.inc");

if (empty ($complete))
	$complete = 0;
if (empty ($team_id))
	$team_id = 1;
$team_data = $svo->readRecordset("select t.*, s.*, g.*, l.* from " . $table['Team'] . " as t, " . $table['G_Team'] . " as g, " . $table['Saison'] . " as s, " . $table['Liga'] . " as l where t.stats_team_id=" . $team_id . " && t.stats_team_global_id=g.stats_team_global_id && l.stats_leagues_id=t.stats_team_league && t.stats_team_season=" . $current_season_id . ";");

#--------------------------------------------------
# CREATE TEMPLATE
#--------------------------------------------------
$t = new Template("templates", "keep");
$t->set_file(array (
	"page" => "svoptik.tpl",
	"navi" => "navi.tpl",
	"ticker" => "ticker.tpl",
	"team" => "game_stats.tpl"
));
#--------------------------------------------------

$t->set_var(array (
	"FORWARD" => ""
));
#--------------------------------------------------
# CREATE NAVIGATION RIGHT
#--------------------------------------------------
$links = "";

$url[0] = array (
	"URL" => "team_stats.php5?team_id=" . $team_id,
	"URL-TEXT" => "Teamstatistiken",
	"ONCLICK" => "",
	"TARGET" => "_self"
);

for ($i = 0; $i < count($url); $i++) {
	$links .= "
	        <tr height=\"15\">
				<td valign=\"top\"><img src=\"standard/spacer.gif\" width=\"20\" height=\"15\" border=\"0\" alt=\"\"></td>
				<td valign=\"top\"><a href=\"" . $url[$i]['URL'] . "\" class=\"sitenav\" target=\"" . $url[$i]['TARGET'] . "\" onClick=\"" . $url[$i]['ONCLICK'] . "\">" . $url[$i]['URL-TEXT'] . "</a></td>
				<td valign=\"top\"><img src=\"standard/spacer.gif\" width=\"20\" height=\"15\" border=\"0\" alt=\"\"></td>
			</tr>
			<tr height=\"15\">
	            <td colspan=\"3\"><img src=\"standard/spacer.gif\" width=\"20\" height=\"15\" border=\"0\" alt=\"\"></td>
			</tr>
	       ";
}
$t->set_var(array (
	"LINKS" => $links,
	"BANNER" => $banner
));
$right_nav = $t->parse("", "navi", true);
#--------------------------------------------------

#--------------------------------------------------
# CREATE ALL-TIME SAME THINGS
#--------------------------------------------------
include_once ("include/sameThings.php5");

#--------------------------------------------------

#--------------------------------------------------
# CREATE CONTENT "STATS"
#--------------------------------------------------
function get_team_data($team_id) {
	global $svo, $table;

	$team_data = $svo->readRecordset("select g.stats_team_global_name, g.stats_team_global_index from " . $table['G_Team'] . " as g, " . $table['Team'] . " as t where g.stats_team_global_id=t.stats_team_global_id && t.stats_team_id=" . $team_id . ";");
	$team = $team_data['stats_team_global_name'] . " " . $team_data['stats_team_global_index'];
	return $team;
}

function check_for_stats($t_id, $g_id) {
	global $svo, $table;

	if ($svo->countRows($svo->doSQL("select stats_game_stats_game_id from " . $table['Stats'] . " where stats_game_stats_game_id=" . $g_id . " && stats_game_stats_team_id=" . $t_id . ";")) > 0)
		$check = 1;
	else
		$check = 0;

	return $check;
}

if (!isset ($game_id))
	$game_id = 0;
if (!isset ($player_id))
	$player_id = 0;

$game_data = game_data($team_id, $game_id);
$game = "
    <table width=\"500\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">
        <tr>
            <td align=\"left\" valign=\"top\" class=\"text\" colspan=\"2\"><b>Spieldaten</b><br><br></td>
        </tr>
        <tr>
            <td align=\"left\" valign=\"top\" class=\"text\">" . $tools->html_replace(get_team_data($game_data['full_data']['stats_game_team_a'])) . "</td>
            <td align=\"left\" valign=\"top\" class=\"text\">" . $game_data['full_data']['stats_game_erg_a'] . "</td>
        </tr>
        <tr>
            <td align=\"left\" valign=\"top\" class=\"text\">" . $tools->html_replace(get_team_data($game_data['full_data']['stats_game_team_b'])) . "</b>
            <td align=\"left\" valign=\"top\" class=\"text\">" . $game_data['full_data']['stats_game_erg_b'] . "</td>
        </tr>
    </table>
  ";
if (check_for_stats($game_data['full_data']['stats_game_team_a'], $game_id))
	$game_stats_a = get_team_playerstats($game_data['full_data']['stats_game_team_a'], $game_id);
else
	$game_stats_a = "Keine Statistiken f&uuml;r " . $tools->html_replace(get_team_data($game_data['full_data']['stats_game_team_a'])) . " verf&uuml;gbar";

if (check_for_stats($game_data['full_data']['stats_game_team_b'], $game_id))
	$game_stats_b = get_team_playerstats($game_data['full_data']['stats_game_team_b'], $game_id);
else
	$game_stats_b = "Keine Statistiken f&uuml;r " . $tools->html_replace(get_team_data($game_data['full_data']['stats_game_team_b'])) . " verf&uuml;gbar";

$t->set_var(array (
	"GAMES" => $game_stats_a . "<br><br>" . $game_stats_b,
	"GAMEDATA" => $game
));
$content = $t->parse("", "team", true);
#--------------------------------------------------

#--------------------------------------------------
# PARSE & OUTPUT
#--------------------------------------------------
$t->set_var(array (
	"sitenav" => "TEAMS -> <a href=\"team.php5?team_id=" . $team_id . "\" class=\"sitenav\" target=\"_self\">" . $tools->html_replace(strtoupper($team_data['stats_team_global_name'] . " - " . $team_data['stats_team_global_class'] . " " . $team_data['stats_team_global_index']
)) . "</a> -> SPIELSTATISTIKEN", "ticker" => $ticker, "content" => $content . "<br>", "navi" => $right_nav,
	"SYNCHRONIZE" => " "));

$t->parse("ALL", "page", true);
$t->p("ALL");
?>
